<%@ Page Language="VB" ValidateRequest="false" MasterPageFile="~/Default.master"
    Title="New Message" %>

<script runat="server">
#Region "Diclaimer"
'Copyright (c) 2006, Mark Bracewell (bracewell at wgg dot com)
'All rights reserved.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met: 
'*

'* Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in the
'documentation and/or other materials provided with the distribution.
'* The name Mark Bracewell may not be used to endorse or promote products
'derived from this software without specific prior written permission. 
'* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#End Region
    Dim replyto As Integer
    Dim topicid As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        replyto = CInt(IIf(String.IsNullOrEmpty(Request("pa")), 0, Request("pa")))
        topicid = CInt(IIf(String.IsNullOrEmpty(Request("to")), 0, Request("to")))
        
        If topicid = 0 Then Response.Redirect("topics.aspx", True)
        
        Dim ti As ClubStarterKit.Data.ForumTopicInfo = ClubStarterKit.Web.Forum.GetTopicInfo(topicid)
                
        Dim isAdmin As Boolean = User.IsInRole("Administrators")
        If Not isAdmin Then
            If (ti.section Or ti.visible = False) Then Response.Redirect("topics.aspx", True)
        End If
        If Not IsPostBack Then
            If replyto <> 0 Then

                Dim msg As ClubStarterKit.Data.ForumMessage = ClubStarterKit.Web.Forum.Load(replyto)
                
                If Not isAdmin Then
                    Dim isLocked As Boolean = ClubStarterKit.Web.Forum.GetThreadLock(msg.threadid)
                    If isLocked Then Response.Redirect("topics.aspx", True)
                End If
                subjectTextBox.Text = HttpUtility.HtmlDecode(msg.subject)
                Headlabel.Text = "Reply to &quot;" & msg.subject & "&quot;"

                If Not String.IsNullOrEmpty(Request("qu")) Then
                    FCKeditor1.Value = String.Format("<div class='quote'>{0} wrote:<br>{1}</div><br>", msg.displayname, msg.body)
                End If
                UpdateButton.Text = "Post Reply"
                UpdateCancelButton.CommandArgument = "thread.aspx?th=" & msg.threadid.ToString
                CheckBoxWatch.Checked = ClubStarterKit.Web.Forum.GetThreadWatch(msg.threadid, New Guid(Membership.GetUser.ProviderUserKey.ToString()))
            Else
                If Not isAdmin Then
                    If ti.locked Then Response.Redirect("topics.aspx", True)
                    AdminOnly.Visible = False
                Else
                    AdminOnly.Visible = True
                End If

                Headlabel.Text = "New thread in &quot;" & ti.topic & "&quot;"
                UpdateCancelButton.CommandArgument = "threads.aspx?to=" & topicid.ToString
            End If
        End If
        Session("FCKeditor:UserFilesPath") = Request.ApplicationPath.ToString & "Files/"
    End Sub

    Protected Sub UpdateCancelButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        If e.CommandName = "Cancel" Then
            Response.Redirect(e.CommandArgument.ToString, True)
        End If
    End Sub

    Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim uGuid As Guid = New Guid(Membership.GetUser().ProviderUserKey().ToString())
        Dim subj As String = subjectTextBox.Text.Trim()
        If String.IsNullOrEmpty(subj) Then
            subj = "No Subject"
        Else
            subj = HttpUtility.HtmlEncode(subj)
        End If
        Dim body As String = FCKeditor1.Value
        Dim msg As ClubStarterKit.Data.ForumMessage = New ClubStarterKit.Data.ForumMessage(subj, body, uGuid, replyto, topicid)
        Dim adminprop As Boolean
        If Page.User.IsInRole("Administrators") Then
            adminprop = AdminOnly.Checked
        Else
            adminprop = False
        End If
        Dim newmsgthreadid As Integer = ClubStarterKit.Web.Forum.Add(msg, adminprop)
        
        ClubStarterKit.Web.Forum.MarkThreadRead(newmsgthreadid, uGuid)
        ClubStarterKit.Web.Forum.SetThreadWatch(newmsgthreadid, uGuid, CheckBoxWatch.Checked)
        
        Response.Redirect("thread.aspx?th=" & newmsgthreadid.ToString, True)
    End Sub
    

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <div class="fullwidth">
            <h3>
                <asp:Label ID="Headlabel" runat="server" Text="Label"></asp:Label></h3>
            Subject:
            <asp:TextBox Columns="40" ID="subjectTextBox" runat="server">
            </asp:TextBox>
            <asp:RequiredFieldValidator ValidationGroup="post" ID="RequiredFieldValidator1"
                runat="server" ControlToValidate="subjectTextBox" ErrorMessage="Need a Subject!">*</asp:RequiredFieldValidator><br />
            <p>
                <FCKeditorV2:FCKeditor ID="FCKeditor1" runat="server" BasePath="~/FCKeditor/" ToolbarSet = "Default" SkinPath = "skins/office2003/" UseBROnCarriageReturn="true" Height="300px"></FCKeditorV2:FCKeditor>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="FCKeditor1"
                    EnableClientScript="False" ErrorMessage="Please add post text." ToolTip="Please add post text."
                    ValidationGroup="post">*</asp:RequiredFieldValidator>
            </p>
            <p>
                <asp:CheckBox ID="CheckBoxWatch" Text="Watch this Thread" runat="server" />
                <br />
                <asp:CheckBox ID="AdminOnly" Text="Make this Tread only visible to Administrators" runat="server" />
            </p>
            
            <asp:Image ID="addimg" runat="server" SkinID="savepost" /> <asp:LinkButton ValidationGroup="post" ID="UpdateButton" runat="server" CausesValidation="True"
                Text="Post" OnClick="UpdateButton_Click">
            </asp:LinkButton>
            |
            <asp:LinkButton ValidationGroup="group1" ID="UpdateCancelButton" runat="server" CausesValidation="False"
                Text="Cancel" CommandName="Cancel" OnCommand="UpdateCancelButton_Command">
            </asp:LinkButton>
        </div>
    </div>
</asp:Content>
